#include "Table.h"
#include <iostream>
#include <iomanip>
#include "Command.h"
#include "IntCell.h"
#include "VarcharCell.h"
#include "NullCell.h"

using std::cout;

Table::Table(void)
{
}


Table::~Table(void)
{
	
}

string Table::getName()
{
	return name;
}

void Table::setName(string pname)
{
	name= pname;
}

vector<string> Table::getColumns()
{
	return columns;
}

int Table::getColumnIndex(string column)
{
	for(unsigned int i=0;i<columns.size();i++)
	{
		if(helper->toUpperString(column) == helper->toUpperString(columns[i]))
			return i;
	}
	return -1;
}

void Table::addColumn(string column)
{
	columns.push_back(column);
}

vector<int> Table::getTypes()
{
	return types;
}

void Table::setTypes(vector<int> newTypes)
{
	types= newTypes;
}

void Table::addType(int type)
{
	types.push_back(type);
}

vector<Row *> Table::getRows()
{
	return rows;
}

void Table::addRow(Row* row)
{
	rows.push_back(row);
}

void Table::setRows(vector<Row *> newRows)
{
	rows= newRows;
}

void Table::removeRows(int index)
{
	rows.erase(rows.begin()+index);
}

void Table::display()
{
	VarcharCell* vCell;
	IntCell* iCell;
	NullCell* nCell;
	
	for(unsigned int i=0;i<columns.size();i++)
	{
		cout << std::setw(10) <<std::right;
		cout << columns[i] << "\t";
	}
	cout << endl;
	vector<int> typeOfCells= this->getTypes();
	for(unsigned int i=0;i<rows.size();i++)
	{
		vector<Cell *> cells= rows[i]->getCells();
		for(unsigned int j=0;j<cells.size();j++)
		{
			cout << std::setw(10) <<std::right;
			if(typeOfCells[j] == VARCHAR)
			{
				vCell= dynamic_cast<VarcharCell *>(cells[j]);								
				if(!vCell)
				{
					nCell= dynamic_cast<NullCell *>(cells[j]);
					if(!nCell)
					{
						cout << "NUll pointer" <<endl;
						return;
					}
					cout << "NULL" << "\t";
					continue;	//missing this cause troule 'cause you continue to extract data from a null cell->>exception
				}
				else
				{
					string data= vCell->getData();
					cout << data << "\t";
				}


			}
			else if(typeOfCells[j] == INT)
			{
				iCell= dynamic_cast<IntCell *>(cells[j]);
				if(!iCell)
				{
					nCell= dynamic_cast<NullCell *>(cells[j]);
					if(!nCell)
					{
						cout << "NUll pointer" <<endl;
						return;
					}
					cout << "NULL" << "\t";
					continue;	//missing this cause troule 'cause you continue to extract data from a null cell->>exception
				}
				int data= iCell->getData();
				cout << data << "\t";
			}
		}
		cout << endl;
	}

	
}
